//
//  DCFinishedController.m
//  JiuyaoMotorcade
//
//  Created by 大橙子 on 2018/11/28.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCFinishedController.h"
#import "DCFinishedHeaderView.h"
#import "DCFinishedCell.h"
#import "DCOrderUploadViewController.h"
#import "DCTaskDetailViewController.h"
//#import "DCFinishedFooterView.h"
@interface DCFinishedController ()
<UITableViewDelegate,UITableViewDataSource,finishedCellDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,assign) NSInteger page;

@property (nonatomic,strong) UIView *backGroundView;
@property (nonatomic,strong) UIImageView *orderImageView;
@property (nonatomic,copy) NSString *imageUrl;
@end

@implementation DCFinishedController
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellStyleDefault;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackgroundColor;
        _tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTableView)];
        _tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMoeData)];
    }
    return _tableView;
}
-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getFinishData];
}
-(void)getFinishData {
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    info[@"taskStatus"] = @"07";
    info[@"page"] = @"1";
    info[@"limit"] = @"10";
    __weak typeof(self) weakself = self;
    [DCServiceTool postWithUrl:getPagerTasksByCarrier_URL params:info success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 0) {
            
            if ([responseObject[@"count"] integerValue] != 0) {
                weakself.dataSource = [DCOrderReceiveModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
                [weakself.view addSubview:weakself.tableView];
                weakself.page = [responseObject[@"page"] integerValue] +1;
                [weakself setUpBgViewIsShow:NO block:nil];
            }else{
                [weakself setUpBgViewIsShow:YES block:^{
                    [weakself getFinishData];
                }];
            }
        }else {
            [weakself showMessage:responseObject[@"msg"] afterDelay:1.0];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)refreshTableView {
    [self.tableView.mj_header endRefreshing];
    [self getFinishData];
}
-(void)reloadMoeData {
    [self.tableView.mj_footer endRefreshing];
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    info[@"taskStatus"] = @"07";
    info[@"page"] = @(self.page);
    info[@"limit"] = @"5";
    __weak typeof(self) weakself = self;
    [DCServiceTool postWithUrl:getPagerTasksByCarrier_URL params:info success:^(id responseObject) {
        DCLog(@"responseObject--%@",responseObject);
        if ([responseObject[@"code"] integerValue] == 0) {
            NSArray *array = [DCOrderReceiveModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            if (array.count != 0) {
                [weakself.dataSource addObjectsFromArray:array];
                weakself.page = [responseObject[@"page"] integerValue] +1;
                [weakself.tableView reloadData];
            }else{
                [weakself showMessage:@"已加载全部数据" afterDelay:1.0];
            }
            
        }else {
            [weakself showMessage:responseObject[@"msg"] afterDelay:1.0];
        }
    } failure:^(NSError *error) {
        
    }];
}
#pragma tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    DCFinishedHeaderView *headerView = [[DCFinishedHeaderView alloc]init];
    headerView.backgroundColor = BackgroundColor;
    DCOrderReceiveModel *model = self.dataSource[section];
    headerView.model = model;
    return headerView;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    return 70;
//}
//-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
//    DCFinishedFooterView *footerView = [[DCFinishedFooterView alloc]init];
//    footerView.backgroundColor = BackgroundColor;
//    DCOrderReceiveModel *model = self.dataSource[section];
//    footerView.model = model;
//    footerView.delegate = self;
//    return footerView;
//}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]init];
    return view;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    DCOrderReceiveModel *model = self.dataSource[section];
    return model.oddArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DCFinishedCell *cell = [DCFinishedCell cellWithTableView:tableView];
    DCOrderReceiveModel *orderModel = self.dataSource[indexPath.section];
    cell.model = orderModel.oddArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [DCFinishedCell getCellHeight];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DCOrderReceiveModel *model = self.dataSource[indexPath.section];
    DCTaskDetailViewController *detailVC = [[DCTaskDetailViewController alloc]initWithOrderID:model.taskId state:@"已完成"];
    [[UIViewController currentViewController].navigationController pushViewController:detailVC animated:YES];
}

#pragma cellDelegate
-(void)selectedWithUploadOrderBtn:(DCOrderReceiveOddModel *)model type:(NSInteger)type
{
    if (type == 0) {
        DCLog(@"运单上传");
        DCOrderUploadViewController *pickerVC = [[DCOrderUploadViewController alloc]initWithTransportModel:model];
        [[UIViewController currentViewController].navigationController pushViewController:pickerVC animated:YES];
    }else{
        __weak typeof(self) weakself = self;
        NSMutableDictionary *info = [NSMutableDictionary dictionary];
//        info[@"transporttationCode"] = model.transportCode;
//        [DCServiceTool postWithUrl:transportUploadDetail_URL params:info success:^(id responseObject) {
//            if ([responseObject[@"code"] integerValue] == 0) {
//
//                [[UIApplication sharedApplication].keyWindow addSubview:weakself.backGroundView];
//                //动画
//                weakself.backGroundView.transform = CGAffineTransformMakeScale(0, 0);
//                [UIView animateWithDuration:.5 animations:^{
//                    weakself.backGroundView.transform = CGAffineTransformIdentity;
//                }];
//                [weakself.orderImageView sd_setImageWithURL:[NSURL URLWithString:[responseObject[@"html"] img]] placeholderImage:IMAGENAME(@"login_logo") options:SDWebImageAllowInvalidSSLCertificates];
//                weakself.imageUrl = [responseObject[@"html"] img];
//            }else{
//
//            }
//        } failure:^(NSError *error) {
//
//        }];
    }
}
-(UIView *)backGroundView {
    if (!_backGroundView) {
        _backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _backGroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5];;
        _backGroundView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tapBgView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgViewDidClick)];
        [_backGroundView addGestureRecognizer:tapBgView];
        
        self.orderImageView = [[UIImageView alloc]init];
        self.orderImageView.backgroundColor = [UIColor whiteColor];
        self.orderImageView.size = CGSizeMake(ScreenWidth-20, 250*HEIGHTSCALE6);
        self.orderImageView.center = _backGroundView.center;
        self.orderImageView.userInteractionEnabled = YES;
        [_backGroundView addSubview:self.orderImageView];
        
        UITapGestureRecognizer *tapOrderImgView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(orderImgViewDidClick)];
        [self.orderImageView addGestureRecognizer:tapOrderImgView];
    }
    return _backGroundView;
}

-(void)bgViewDidClick {
    UIView *view = self.orderImageView.superview;
    view.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:.5 animations:^{
        view.transform = CGAffineTransformMakeScale(0.0000000001, 0.00000001);
    }completion:^(BOOL finished) {
        [view removeFromSuperview];
    }];
}
-(void)orderImgViewDidClick {
    
    if (!Str_IsEmpty(self.imageUrl)) {
        [PicShow showImage:self.orderImageView url:self.imageUrl image:nil];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
